<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: easyui-header('角色分配用户')" />
</head>
<body class="white-bg">
	<div class="container-div">
		<div class="row">
			<div class="col-sm-12 search-head">
				<div class="easyui-panel">
					<form id="role-form">
					    <input type="hidden" id="roleId" name="roleId" th:value="${role.roleId}">
						<div class="select-list">
							<ul>
								<li>
									<label>登录名称：</label>
									<input id="loginName" type="text" name="loginName" class="easyui-textbox" data-options="prompt:'请输入登录名称'"/>
								</li>
								<li>
									<label>手机号码：</label>
									<input id="phonenumber" type="text" name="phonenumber" class="easyui-textbox" data-options="prompt:'请输入手机号码'"/>
								</li>
								<li>
									<a class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="searchAuthUser()" >查询</a>
									<a class="easyui-linkbutton" data-options="iconCls:'icon-clear'" onclick="resetAuthUserForm()" >重置</a>
								</li>
							</ul>
						</div>
					</form>
				</div>
			</div>
	        <div class="col-sm-12 search-body">
				<div id="toolbar" role="group">
		            <a class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="selectUser()" shiro:hasPermission="system:role:add">添加用户</a>
		            <a class="easyui-linkbutton" data-options="iconCls:'icon-delete'" onclick="cancelAuthUserAll()" shiro:hasPermission="system:role:remove">批量取消授权</a>
		            <a class="easyui-linkbutton" data-options="iconCls:'icon-extjs-cancel'" onclick="closeItem()">关闭</a>
		        </div>
				<table id="dg" class="easyui-datagrid"></table>
			</div>
		</div>
	</div>
	<th:block th:include="include :: easyui-footer" />
 	<script th:inline="javascript">
 		var removeFlag = [[${@permission.hasPermi('system:role:remove')}]];
		var datas = [[${@dict.getType('sys_normal_disable')}]];
    </script>
	<script th:inline="none">
		var prefix = ctx + "system/role/authUser";
		$(function() {
			var options = {
		        url: prefix + "/allocatedList",
		        createUrl: prefix + "/add",
		        updateUrl: prefix + "/edit/{id}",
		        removeUrl: prefix + "/remove",
		        exportUrl: prefix + "/export",
		        importUrl: prefix + "/importData",
		        importTemplateUrl: prefix + "/importTemplate",
		        queryParams: {
		        	"roleId" : $("#roleId").val()
		        },
		        modalName: "用户",
		        idField: 'userId',
		        sortName: "createTime",
		        sortOrder: "desc",
		        columns: [[
					{
						field:'ck',
						checkbox:true,
						width:30
					},
			        {
			            field: 'userId',
			            title: '用户ID',
		            	align: 'center',
		            	hidden:true,
						width: 20
			        },
			        {
			            field: 'loginName',
			            title: '登录名称',
			            sortable: true,
			            align: 'center',
		          		width: 80
			        },
			        {
			            field: 'userName',
			            title: '用户名称',
			            align: 'center',
		          		width: 60
			        },
			        {
			            field: 'email',
			            title: '邮箱',
			            align: 'center',
		          		width: 100
			        },
			        {
			            field: 'phonenumber',
			            title: '手机',
			            align: 'center',
		          		width: 60
			        },
			        {
			        	field: 'status',
			        	title: '用户状态',
			        	align: 'center',
			        	width: 60,
			        	formatter: function (value, row, index) {
			        		return $.table.selectDictLabel(datas, value);
			        	}
			        },
			        {
			            field: 'createTime',
			            title: '创建时间',
			            sortable: true,
			            align: 'center',
		          		width: 100
			        },
			        {
			        	field:'action',
			            title: '操作',
			            align: 'center',
			            width: 100,
			            formatter: function(value, row, index) {
			                var actions = [];
			                actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="cancelAuthUser(\'' + row.userId + '\')"><i class="fa fa-remove"></i>取消授权</a> ');
			                return actions.join('');
			            }
			        }
				]]
			};
		    $.easyUIDataGrid.init(options);
		});
		//查询
		function searchAuthUser(){
			var params = $.common.formToJSON("role-form");
           	$('#dg').datagrid('load', params).datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		}
		//重置
		function resetAuthUserForm(){
			$("#loginName").textbox("setValue","");
			$("#phonenumber").textbox("setValue","");
			var params = $.common.formToJSON("role-form");
           	$('#dg').datagrid('load', params).datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		}
		/* 分配用户-选择用户 */
		function selectUser() {
			var url = prefix + '/selectUser/' + $("#roleId").val();
		 	// 皮肤缓存
        	var skin = storage.get("skin");
        	var panelWindowTheme = "";
        	var panelWindowHeaderTheme = "";
        	var panelWindowBodyTheme = "";
        	// 本地主题优先，未设置取系统配置
        	if($.common.isNotEmpty(skin)){
        		panelWindowTheme = "easyui-window-" + skin.split('|')[0];
        		panelWindowHeaderTheme = "easyui-window-header-" + skin.split('|')[0];
        		panelWindowBodyTheme = "easyui-window-body-" + skin.split('|')[0];
        	}
        	var dialogObj = $('<div id="selectUserDialog" style="overflow-x:hidden;"/>');
        	dialogObj.dialog({
        		href: url,
				iconCls: "icon-add",
				width : 800,
				height : ($(window).height() - 50),
				modal : true,
				maximizable: true,
				title : "选择用户",
				buttons : [ {
					text : '提交',
					iconCls : 'icon-save',
					handler : function() {
						var selectRows = $("#user_dg").datagrid('getSelections');
			    		if(selectRows.length == 0){
			    			$.messager.alert("提示", "请至少选择一条记录！", 'warning');
			    			return false;
			    		}
			    		var idsArr = new Array();
			 	        for(var i=0; i<selectRows.length; i++){
			 	        	idsArr.push(selectRows[i]["userId"]);
			 	        }
						var data = { "roleId": $("#roleId").val(), "userIds": idsArr.join() };
						var config = {
		        	        url: ctx + "system/role/authUser/selectAll",
		        	        type: "post",
		        	        dataType: "json",
		        	        data: data,
		        	        beforeSend: function () {
		        	        	$.modal.loading("正在处理中，请稍后...");
		        	        },
		        	        success: function(result) {
		        	        	if (result.code == web_status.SUCCESS) {
		                           	dialogObj.dialog('destroy');
		                        	$.messager.show({
		        						title : '提示',
		        						msg : result.msg,
		        						timeout: 1000
		        					});
		                        	var params = $.common.formToJSON("role-form");
		                           	$('#dg').datagrid('load', params).datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		                        } else if (result.code == web_status.WARNING) {
		                            $.messager.alert("提示", result.msg, 'warning');
		                        }  else {
		                        	$.messager.alert("提示", result.msg, 'error');
		                        }
		                    	$.modal.closeLoading();
		        	        }
		        	    };
		        	    $.ajax(config);
					}
				},{
					text : '关闭',
					iconCls : 'icon-cancel',
					handler : function() {
						dialogObj.dialog('destroy');
					}
				} ],
				onOpen : function(){
					$("#selectUserDialog").parents(".window").addClass(panelWindowTheme);
					$("#selectUserDialog").addClass(panelWindowBodyTheme);
					$("#selectUserDialog").siblings(".window-header").find(".panel-title").addClass(panelWindowHeaderTheme);
				},
				onClose : function() {
					dialogObj.dialog('destroy');
				}
			});
		}
		
		/* 分配用户-批量取消授权 */
		function cancelAuthUserAll(userId) {
			var selectRows = $("#dg").datagrid('getSelections');
    		if(selectRows.length == 0){
    			$.messager.alert("提示", "请至少选择一条记录！", 'warning');
    			return false;
    		}
    		var idsArr = new Array();
 	        for(var i=0; i<selectRows.length; i++){
 	        	idsArr.push(selectRows[i]["userId"]);
 	        }
			$.messager.confirm("确认", "确认要取消选中的" + selectRows.length + "条数据授权吗？", function (isDelete) {
 	        	if (isDelete) {
 	        		var data = { "roleId": $("#roleId").val(), "userIds": idsArr.join() };
 	    			var config = {
	        	        url: prefix + "/cancelAll",
	        	        type: "post",
	        	        dataType: "json",
	        	        data: data,
	        	        beforeSend: function () {
	        	        	$.modal.loading("正在处理中，请稍后...");
	        	        },
	        	        success: function(result) {
	        	        	if (result.code == web_status.SUCCESS) {
	                        	$.messager.show({
	        						title : '提示',
	        						msg : result.msg,
	        						timeout: 1000
	        					});
	                        	var params = $.common.formToJSON("role-form");
	                           	$('#dg').datagrid('load', params).datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
	                        } else if (result.code == web_status.WARNING) {
	                            $.messager.alert("提示", result.msg, 'warning');
	                        }  else {
	                        	$.messager.alert("提示", result.msg, 'error');
	                        }
	                    	$.modal.closeLoading();
	        	        }
	        	    };
	        	    $.ajax(config);
 	        	}
 	        });
		}
		
		/* 分配用户-取消授权 */
		function cancelAuthUser(userId) {
			$.messager.confirm("确认", "确认要取消该用户角色吗？", function (isDelete) {
 	        	if (isDelete) {
 	        		var config = {
	        	        url: prefix + "/cancel",
	        	        type: "post",
	        	        dataType: "json",
	        	        data: { "roleId": $("#roleId").val(), "userId": userId },
	        	        beforeSend: function () {
	        	        	$.modal.loading("正在处理中，请稍后...");
	        	        },
	        	        success: function(result) {
	        	        	if (result.code == web_status.SUCCESS) {
	                        	$.messager.show({
	        						title : '提示',
	        						msg : result.msg,
	        						timeout: 1000
	        					});
	                        	var params = $.common.formToJSON("role-form");
	                           	$('#dg').datagrid('load', params).datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
	                        } else if (result.code == web_status.WARNING) {
	                            $.messager.alert("提示", result.msg, 'warning');
	                        }  else {
	                        	$.messager.alert("提示", result.msg, 'error');
	                        }
	                    	$.modal.closeLoading();
	        	        }
	        	    };
	        	    $.ajax(config);
 	        	}
 	        });
		}
	</script>
</body>
</html>